package zyd.springboot.springbootmybatis.service.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zyd.springboot.springbootmybatis.entity.User;
import zyd.springboot.springbootmybatis.mapper.UserMapper;
import zyd.springboot.springbootmybatis.service.UserServiceDemo;

@Service
public class UserServiceDemoImpl implements UserServiceDemo {
    /*因为UserMapper是接口，假如没这个，idea会提示mapper没有实现类，标红
   而springboot会根据动态代理，反射机制，自动创建对应接口的实例*/
    @Autowired(required = false)
    private UserMapper userMapper;
    @Override
    public boolean registDemo(User user) {
        User userData=userMapper.findUserByUname(user.getUsername());
        if(userData!=null){
            System.out.println("用户已经注册，注册失败！");
            return  false;
        }
        int row=userMapper.insertUser(user);
        if(row>=1){
            System.out.println("注册成功!");
            return true;        }
        else {
            System.out.println("注册失败!");
            return false;
        }
    }

    @Override
    public User loginDemo(User user) {
        User userData=userMapper.findUserByUname(user.getUsername());
        if(userData==null){
            System.out.println("用户未注册，登陆失败!");
            return null;
        }
        if(!(user.getPassword().equals(userData.getPassword()))){//密码是否想能
            System.out.println("密码错误，登陆失败!");
            return null;
        }
        userData.setPassword(null);
        System.out.println("登陆成功："+userData);
        return userData;
    }
}
